home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14328 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.8 KB

  1. Path: news2.cais.com!news
  2. From: vanyo@ezaccess.net (Bill Vanyo)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: WEIRD WEIRD Help Please a.s.a.p.:head pointers
  5. Date: Fri, 12 Apr 1996 14:23:36 GMT
  6. Organization: Capital Area Internet Service, Inc.
  7. Message-ID: <4kogpt$752@news2.cais.com>
  8. References: <4km0vb$o04@badger.wmin.ac.uk>
  9. NNTP-Posting-Host: ppp-166.ezaccess.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. (I am re-posting this; believe first post got lost)
  13. Idoia Lertxundi <gsoec@wmin.ac.uk> wrote:
  14.  
  15. >Hi C sufferers/lovers,
  16.  
  17. >I am working in the C program and I have come accross a weird prob.
  18. >Rather than sending you code I will explain it in English as I have
  19. >accurately located the problem.
  20.  
  21. The code would have helped enormously.  I feel like I'm geussing
  22. without it, but think I see the problem:
  23.  
  24. >I have a list which is being created dinamycally and a pointer pointing to
  25. >the head of the list. 
  26.  
  27. >ptr=AllocateSpaceforptr();
  28. >head=ptr;
  29.  
  30. > head  ptr....ptr
  31. > |   !   !  |
  32. > {}-{}-{}-NULL
  33.  
  34. >  1  2  3  4
  35.  
  36. >When I have put data in node number 3 I allocate with a malloc call
  37. >node number 4 and as the function do not find suitable data to put 
  38. >in number 4 after a for loop I assign it to NULL. i.e.
  39.  
  40. It would be better not to do that malloc until you know you need it.
  41.  
  42. >     ptr=(FILEMODULE *)NULL;
  43.  
  44. This is not affecting any part of the list pointed to by head.
  45.  
  46. You should be setting the link field of the third node to NULL.
  47.  
  48. My geuss is that your using only the two pointer variables head and
  49. ptr, with head never changing, so that to build the list you're doing
  50. something like
  51.    malloc(ptr->link);
  52.    ptr=ptr->link;
  53. Let's say you just added the fourth node this way.  At this point the
  54. variable ptr and the link field of the third node are both pointing to
  55. the newly allocated fourth node. If you decide you want only four
  56. nodes, do
  57.    ptr->link=NULL;
  58. If, on the other hand, you now decide you want only three nodes, you
  59. need to set the link field of the third node to NULL.  No neat way to
  60. do this.  Doing
  61.    ptr=NULL;
  62. does not affect the link field of the third node -- this still points
  63. to the newly allocated fourth node.
  64.  
  65. Am I geussing right?
  66.  
  67. >When I do printfs I see that the list of ptr corresponds to what I was
  68. >expecting the above picture when I print out the content of the head list
  69. >which points to the begining of the ptr list the result is unexpected.
  70. >i.e.
  71.  
  72. >head  
  73. > |   
  74. > {}-{}-{}-[] the last node instead of being NULL is not defined.
  75.  
  76. >  1  2  3  4
  77.  
  78. >By not defined I mean that is a if after malloc there has not been anything
  79. >put in there although I put the NULL which shows perfectly well in the ptr
  80. >list. Somehow the head list is not receiving the NULL asigment. WHY??
  81.  
  82. >Any ideas you please mail me. I am sending this to a Newsgroup too.
  83.  
  84. >Thank you.
  85.  
  86. >-- 
  87. >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. >Ms Idoia Lertxundi                              .            ,
  89. >                                                            .:/
  90. >e-mail: gsoec@wmin.ac.uk                           .      ,,///;,   ,;/
  91. >URL   : http://www.wmin.ac.uk/~gsoec/                .   o:::::::;;///
  92. >                                                        >::::::::;;\\\
  93. >                                                         `'\\\\\'" `;\
  94. >                                                            `;\
  95. >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. >"All philosophy," I told her, "is based on two things only:
  97. > curiosity and poor eyesight; if you had better eyesight you could see
  98. > perfectly well whether or not these stars are solar systems, and if  
  99. > you were less curious you wouldn't care about knowing, which amounts 
  100. > to the same thing. The trouble is, we want to know more than we can see." 
  101.  
  102. >Bernard de Fontanelle, Conversations on the Plurality of Worlds.
  103.  
  104.  
  105.  
  106.